home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / dev / c / CLib-SDI.lha / CLib-SDI / libsource / libinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-25  |  7.1 KB  |  240 lines

  1. #ifndef EXAMPLE_LIBINIT_C
  2. #define EXAMPLE_LIBINIT_C
  3.  
  4. /* Programmheader
  5.  
  6.     Name:        libinit.c
  7.     Main:        example
  8.     Versionstring:    $VER: libinit.c 1.0 (25.06.2000)
  9.     Author:        SDI
  10.     Distribution:    Freeware
  11.     Description:    all the library initialization stuff
  12.  
  13.  1.0   25.06.00 : first version
  14. */
  15.  
  16. #include <proto/exec.h>
  17. #include <exec/resident.h>
  18. #include <exec/initializers.h>
  19. #include <intuition/intuitionbase.h>
  20. #include <exec/execbase.h>
  21. #include "libinfo.h"
  22.  
  23. /************************************************************************/
  24.  
  25. /* First executable routine of this library; must return an error
  26.    to the unsuspecting caller */
  27. LONG ReturnError(void)
  28. {
  29.   return -1;
  30. }
  31.  
  32. /************************************************************************/
  33.  
  34. struct LibInitData {
  35.  UBYTE i_Type;     UBYTE o_Type;     UBYTE  d_Type;    UBYTE p_Type;
  36.  UBYTE i_Name;     UBYTE o_Name;     STRPTR d_Name;
  37.  UBYTE i_Flags;    UBYTE o_Flags;    UBYTE  d_Flags;    UBYTE p_Flags;
  38.  UBYTE i_Version;  UBYTE o_Version;  UWORD  d_Version;
  39.  UBYTE i_Revision; UBYTE o_Revision; UWORD  d_Revision;
  40.  UBYTE i_IdString; UBYTE o_IdString; STRPTR d_IdString;
  41.  ULONG endmark;
  42. };
  43.  
  44. /************************************************************************/
  45. extern const ULONG LibInitTable[4]; /* the prototype */
  46.  
  47. /* The library loader looks for this marker in the memory
  48.    the library code and data will occupy. It is responsible
  49.    setting up the Library base data structure. */
  50. const struct Resident RomTag = {
  51.   RTC_MATCHWORD,                   /* Marker value. */
  52.   (struct Resident *)&RomTag,      /* This points back to itself. */
  53.   (struct Resident *)LibInitTable, /* This points somewhere behind this marker. */
  54.   RTF_AUTOINIT,                    /* The Library should be set up according to the given table. */
  55.   VERSION,                         /* The version of this Library. */
  56.   NT_LIBRARY,                      /* This defines this module as a Library. */
  57.   0,                               /* Initialization priority of this Library; unused. */
  58.   LIBNAME,                         /* Points to the name of the Library. */
  59.   IDSTRING,                        /* The identification string of this Library. */
  60.   (APTR)&LibInitTable              /* This table is for initializing the Library. */
  61. };
  62.  
  63. /************************************************************************/
  64.  
  65. /* The mandatory reserved library function */
  66. ULONG LibReserved(void)
  67. {
  68.   return 0;
  69. }
  70.  
  71. /* Open the library, as called via OpenLibrary() */
  72. ASM(struct Library *) LibOpen(REG(a6, struct ExampleBaseP * ExampleBase))
  73. {
  74.   /* Prevent delayed expunge and increment opencnt */
  75.   ExampleBase->exb_LibNode.lib_Flags &= ~LIBF_DELEXP;
  76.   ExampleBase->exb_LibNode.lib_OpenCnt++;
  77.  
  78.   return &ExampleBase->exb_LibNode;
  79. }
  80.  
  81. #ifdef BASE_GLOBAL
  82. struct ExecBase *      SysBase       = 0;
  83. struct IntuitionBase * IntuitionBase = 0;
  84. struct ExampleBase *   ExampleBase   = 0;
  85.  
  86. static void MakeGlobalLibs(struct ExampleBaseP *exampleBase)
  87. {
  88.   IntuitionBase = exampleBase->exb_IntuitionBase;
  89.   ExampleBase   = (struct ExampleBase *) exampleBase;
  90. }
  91. static void MakeGlobalSys(struct ExampleBaseP *exampleBase)
  92. {
  93.   SysBase = exampleBase->exb_SysBase;
  94. }
  95. #endif
  96.  
  97. /* Closes all the libraries opened by LibInit() */
  98. static void CloseLibraries(struct ExampleBaseP * ExampleBase)
  99. {
  100. #ifndef BASE_GLOBAL
  101.   struct ExecBase *SysBase = ExampleBase->exb_SysBase;
  102. #endif
  103.  
  104.   if(ExampleBase->exb_IntuitionBase)
  105.     CloseLibrary((struct Library *) ExampleBase->exb_IntuitionBase);
  106. }
  107.  
  108. /* Expunge the library, remove it from memory */
  109. ASM(SEGLISTPTR) LibExpunge(REG(a6, struct ExampleBaseP * ExampleBase))
  110. {
  111. #ifndef BASE_GLOBAL
  112.   struct ExecBase *SysBase = ExampleBase->exb_SysBase;
  113. #endif
  114.  
  115.   if(!ExampleBase->exb_LibNode.lib_OpenCnt)
  116.   {
  117.     SEGLISTPTR seglist;
  118.  
  119.     seglist = ExampleBase->exb_SegList;
  120.  
  121.     CloseLibraries(ExampleBase);
  122.  
  123.     /* Remove the library from the public list */
  124.     Remove((struct Node *) ExampleBase);
  125.  
  126.     /* Free the vector table and the library data */
  127.     FreeMem((STRPTR) ExampleBase - ExampleBase->exb_LibNode.lib_NegSize,
  128.     ExampleBase->exb_LibNode.lib_NegSize +
  129.     ExampleBase->exb_LibNode.lib_PosSize);
  130.  
  131.     return seglist;
  132.   }
  133.   else
  134.     ExampleBase->exb_LibNode.lib_Flags |= LIBF_DELEXP;
  135.  
  136.   /* Return the segment pointer, if any */
  137.   return 0;
  138. }
  139.  
  140. /* Close the library, as called by CloseLibrary() */
  141. ASM(SEGLISTPTR) LibClose(REG(a6, struct ExampleBaseP * ExampleBase))
  142. {
  143.   if(!(--ExampleBase->exb_LibNode.lib_OpenCnt))
  144.   {
  145.     if(ExampleBase->exb_LibNode.lib_Flags & LIBF_DELEXP)
  146.       return LibExpunge(ExampleBase);
  147.   }
  148.   return 0;
  149. }
  150.  
  151. /* Initialize library */
  152. ASM(struct Library *) LibInit(REG(a0, SEGLISTPTR seglist),
  153. REG(d0, struct ExampleBaseP * ExampleBase), REG(a6, struct ExecBase *SysBase))
  154. {
  155. #ifdef _M68060
  156.   if(!(SysBase->AttnFlags & AFF_68060))
  157.     return 0;
  158. #elif defined (_M68040)
  159.   if(!(SysBase->AttnFlags & AFF_68040))
  160.     return 0;
  161. #elif defined (_M68030)
  162.   if(!(SysBase->AttnFlags & AFF_68030))
  163.     return 0;
  164. #elif defined (_M68020)
  165.   if(!(SysBase->AttnFlags & AFF_68020))
  166.     return 0;
  167. #endif
  168.  
  169.   /* Remember stuff */
  170.   ExampleBase->exb_SegList = seglist;
  171.   ExampleBase->exb_SysBase = SysBase;
  172.  
  173. #ifdef BASE_GLOBAL
  174.   MakeGlobalSys(ExampleBase);
  175. #endif
  176.  
  177.   if((ExampleBase->exb_IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37)))
  178.   {
  179. #ifdef BASE_GLOBAL
  180.     MakeGlobalLibs(ExampleBase);
  181. #endif
  182.     return &ExampleBase->exb_LibNode;
  183.   }
  184.  
  185.   /* Free the vector table and the library data */
  186.   FreeMem((STRPTR) ExampleBase - ExampleBase->exb_LibNode.lib_NegSize,
  187.   ExampleBase->exb_LibNode.lib_NegSize +
  188.   ExampleBase->exb_LibNode.lib_PosSize);
  189.  
  190.   return 0;
  191. }
  192.  
  193. /************************************************************************/
  194.  
  195. /* This is the table of functions that make up the library. The first
  196.    four are mandatory, everything following it are user callable
  197.    routines. The table is terminated by the value -1. */
  198.  
  199. static const APTR LibVectors[] = {
  200.   (APTR) LibOpen,
  201.   (APTR) LibClose,
  202.   (APTR) LibExpunge,
  203.   (APTR) LibReserved,
  204.   (APTR) LIBex_TestRequest,
  205.   (APTR) LIBex_TestRequest2A,
  206.   (APTR) -1
  207. };
  208.  
  209. static const struct LibInitData LibInitData = {
  210. #ifdef __VBCC__    /* VBCC does not like OFFSET macro */
  211.  0xA0,  8, NT_LIBRARY,                0,
  212.  0x80, 10, LIBNAME,
  213.  0xA0, 14, LIBF_SUMUSED|LIBF_CHANGED, 0,
  214.  0x90, 20, VERSION,
  215.  0x90, 22, REVISION,
  216.  0x80, 24, IDSTRING,
  217. #else
  218.  0xA0, (UBYTE) OFFSET(Node,    ln_Type),      NT_LIBRARY,         0,
  219.  0x80, (UBYTE) OFFSET(Node,    ln_Name),      LIBNAME,
  220.  0xA0, (UBYTE) OFFSET(Library, lib_Flags),    LIBF_SUMUSED|LIBF_CHANGED, 0,
  221.  0x90, (UBYTE) OFFSET(Library, lib_Version),  VERSION,
  222.  0x90, (UBYTE) OFFSET(Library, lib_Revision), REVISION,
  223.  0x80, (UBYTE) OFFSET(Library, lib_IdString), IDSTRING,
  224. #endif
  225.  0
  226. };
  227.  
  228. /* The following data structures and data are responsible for
  229.    setting up the Library base data structure and the library
  230.    function vector.
  231. */
  232. const ULONG LibInitTable[4] = {
  233.   (ULONG)sizeof(struct ExampleBaseP), /* Size of the base data structure */
  234.   (ULONG)LibVectors,             /* Points to the function vector */
  235.   (ULONG)&LibInitData,           /* Library base data structure setup table */
  236.   (ULONG)LibInit                 /* The address of the routine to do the setup */
  237. };
  238.  
  239. #endif /* EXAMPLE_LIBINIT_C */
  240.